package com.xinke.module.system.controller.admin.sms;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.xinke.component.common.pojo.PageParam;
import com.xinke.component.common.pojo.PageResult;
import com.xinke.component.common.pojo.Result;
import com.xinke.component.common.util.object.BeanUtils;
import static com.xinke.component.common.pojo.Result.success;

import com.xinke.component.excel.excel.core.util.ExcelUtils;

import com.xinke.component.web.apilog.core.annotation.ApiAccessLog;
import static com.xinke.component.web.apilog.core.enums.OperateTypeEnum.*;

import com.xinke.module.system.model.vo.sms.*;
import com.xinke.module.system.model.entity.sms.SmsCodeDO;
import com.xinke.module.system.service.sms.SmsCodeService;

@Tag(name = "管理后台 - 手机验证码")
@RestController
@RequestMapping("/system/sms-code")
@Validated
public class SmsCodeController {

    @Resource
    private SmsCodeService smsCodeService;

    @PostMapping("/create")
    @Operation(summary = "创建手机验证码")
    @PreAuthorize("@ss.hasPermission('system:sms-code:create')")
    public Result<Long> createSmsCode(@Valid @RequestBody SmsCodeSaveReqVO createReqVO) {
        return success(smsCodeService.createSmsCode(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新手机验证码")
    @PreAuthorize("@ss.hasPermission('system:sms-code:update')")
    public Result<Boolean> updateSmsCode(@Valid @RequestBody SmsCodeSaveReqVO updateReqVO) {
        smsCodeService.updateSmsCode(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除手机验证码")
    @Parameter(name = "smsId", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('system:sms-code:delete')")
    public Result<Boolean> deleteSmsCode(@RequestParam("smsId") Long smsId) {
        smsCodeService.deleteSmsCode(smsId);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得手机验证码")
    @Parameter(name = "smsId", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('system:sms-code:query')")
    public Result<SmsCodeRespVO> getSmsCode(@RequestParam("smsId") Long smsId) {
        SmsCodeDO smsCode = smsCodeService.getSmsCode(smsId);
        return success(BeanUtils.toBean(smsCode, SmsCodeRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得手机验证码分页")
    @PreAuthorize("@ss.hasPermission('system:sms-code:query')")
    public Result<PageResult<SmsCodeRespVO>> getSmsCodePage(@Valid SmsCodePageReqVO pageReqVO) {
        PageResult<SmsCodeDO> pageResult = smsCodeService.getSmsCodePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, SmsCodeRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出手机验证码 Excel")
    @PreAuthorize("@ss.hasPermission('system:sms-code:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportSmsCodeExcel(@Valid SmsCodePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<SmsCodeDO> list = smsCodeService.getSmsCodePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "手机验证码.xls", "数据", SmsCodeRespVO.class,
                        BeanUtils.toBean(list, SmsCodeRespVO.class));
    }

}